import java.util.Comparator;

class CompareAge implements Comparator<Person> {
    public int compare(Person per1, Person per2) {
        return per1.age - per2.age;
    }
}

class Person {
    private String name;
    public int age;

    public Person(String name, int age) {
        this.name = name == null ? "" : name;
        this.age = age;
    }

    public String toString() {
        return this.name + "," + this.age;
    }
}

class Student extends Person {
    private int id; // 学号

    public Student(String name, int age, int id) {
        super(name, age);
        this.id = id;
    }

    public String toString() {
        return super.toString() + "," + this.id;
    }
}

public class Person_ex {
    /*
     * 补充位置
     * public static <T> T min(T[] objs, Comparator<? super T> comp)
     * 说明：返回objs数组元素最小值，委托comp比较器提供比较T类对象大小的compare(T,T)方法
     * 用泛型实现更通用的功能，代码中不出现Person类和MyDate类
     */
    public static <T> T min(T[] objs, Comparator<? super T> comp){
        if (objs.length == 1) return objs[0];
        T youngestPer = objs[0];
        for (int i = 1;i < objs.length;i++){
        youngestPer = comp.compare(objs[i], youngestPer) < 0 ? youngestPer : objs[i];
        }
        return youngestPer;
    }
    public static void main(String[] args) {
        Person[] pers = { new Person("李小明", 19),
                new Person("张莉", 18),
                new Person("朱红", 20) };
        System.out.println(min(pers, new CompareAge()));
        Person[] pers2 = { new Person("李小明", 19),
                new Student("张莉", 18, 2022001),
                new Student("朱红", 20, 2022002) };
        System.out.println(min(pers2, new CompareAge()));
        Student[] stus = { new Student("张莉", 18, 2022001),
                new Student("朱红", 20, 2022002) };
        System.out.println(min(stus, new CompareAge()));
        Object[] os = { new Person("李小明", 19),
                new Student("张莉", 18, 2022001) };
        // System.out.println(min(os,new CompareAge()));//思考，这行代码为什么报错？！
    }
}
/*
 * 测试用例说明：
 * 输入：无
 * 输出：
 * 朱红,20
 * 朱红,20,2022002
 * 朱红,20,2022002
 */
